/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.relatorios.dao;

import java.sql.SQLException;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import br.com.relatorios.entity.Usuario;

/**
 *
 * @author Juliano
 */
public class UsuarioDAO extends BaseDAO<Usuario> {
    
	public Boolean verificaLoginExistente(String login){
		String sql = "select u from Usuario u where u.login = '"+login+"'";
		Query query = entityManager.createQuery(sql);
		Usuario user = null;
		try {
			user = (Usuario) query.getSingleResult();
	    } catch (NoResultException e) {  
	    	user = null;  
	    }		
		if(user == null){
			return true;
		} else {
			return false;
		}
	}
	
	
	public Boolean verificaEmailExistente(String email){
		String sql2 = "select u from Usuario u where u.email = '"+email+"'";
		String mandasenha = "select u.senha from Usuario u.email = '"+email+"'";
		Query q = entityManager.createQuery(sql2); 
		Usuario user = null;
		try {
			user = (Usuario) q.getSingleResult();
	    } catch (NoResultException e) {  
	    	user = null;  
	    } 
		if(user == null){
			return true;
		} else {
			return false;
		}
	}
	
    public Usuario login(String user, String pass) throws IllegalArgumentException, SQLException {      
        Query query = entityManager.createQuery("select u from Usuario u where u.login = :user and u.senha = :pass");
        query.setParameter("user", user/*.toUpperCase()*/);
        query.setParameter("pass", pass/*.toUpperCase()*/);
        try {
            return (Usuario) query.getSingleResult();
        } catch(Exception ex){
            return null;
        }
    }
    
}
